<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Member as MemberModel;
use think\Db;
use think\Controller;

/**
 * 微博登录控制器
 * Class Weibo
 * @package app\home\controller
 */
class Vercode extends Controller
{
    /**
     * [login  微博登录]
     * @return json
     * @author [C。]
     */
    public function verify()
    {
        $param = input('param.');
        $model = new MemberModel;
        $map = [];
        $map1 = [];
        try {
            $member = $model->findOne(['tel'=>$param['tel']],'id,token');
            if(empty($member)){
                return admin_json('-1', '该用户不存在!');
            }
            $map['tel'] = $param['tel'];
            $map['code'] = $param['code'];

            $map1['tel'] = $param['tel'];
            if(!check_mobile($param['tel'])){
                return admin_json('-1', '手机号或验证码错误!');
            }
            $result = Db::name('vercode')->where($map)->find();
            if (!empty($result)) {
                if ($result['status'] == 2) {
                    return admin_json('-3', '验证码已使用');
                }
                if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                    return admin_json('-2', '验证码已过期');
                }
                Db::name('vercode')->where($map)->update(['status' => 2]);
            } else {
                return admin_json('-1', '手机号或验证码错误');
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/Vercode/verify');
            return admin_json(-1, '验证失败'.$e->getMessage());
        }
        return admin_json(1, '验证成功',$member['token']);
    }
    /**
     * [editpass  修改密码]
     * @return json
     * @author [C。]
     */
    public function editpass()
    {
        $param = input('param.');
        $model = new MemberModel;
        try {
            if(empty($param['token'])){
                return admin_json(-1, '修改失败！');
            }
            $member = $model->findOne(['token'=>$param['token']],'id');
            if(empty($member)){
                return admin_json(-1, '修改失败！');
            }
            $model->editData(['id'=>$member['id'],'pswd'=>md5($param['pswd'])]);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/Vercode/editpass');
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        return admin_json(1, '修改成功');
    }
}